﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GridLoaction
{
    public class GridPacker
    {
        //public class Rect
        //{
        //    /// <summary>
        //    /// 左上角的点的X坐标
        //    /// </summary>
        //    public double X { get; set; }
        //    /// <summary>
        //    /// 左上角的点的Y坐标
        //    /// </summary>
        //    public double Y { get; set; }
        //    public double Width { get; set; }
        //    public double Height { get; set; }

        //    public double CenterX { get;private set; }
        //    public double CenterY { get;private set; }

        //    public Rect(double x,double y,double width,double height)
        //    {
        //        X = x;
        //        Y = y;
        //        Width = width;
        //        Height = height;
        //        CenterX = x+width/2;
        //        CenterY = y+height/2;
        //    }

        //}

        public class Container
        {
            public int X { get; private set; }
            public int Y { get;private set; }
            public int  Width { get;private set; }
            public int  Height { get;private set; }
            public int ValidWidth { get;private set; }
            public int ValidHeight { get;private set; }
            public int ValidX { get; private set; }
            public int ValidY { get;private set; }
            public int WallClearance { get; private set; }

            public Container(int x,int y,int width,int height,int wallClearance)
            {
                X = x;
                Y = y;
                Width=width;
                Height = height;
                WallClearance = wallClearance;
                ValidX = x+wallClearance; 
                ValidY = y+wallClearance;
                ValidWidth = Width-2*wallClearance;
                ValidHeight = Height-2*wallClearance;
            }

            public Rectangle GetValidRectangle()
            {
                Rectangle rect = new Rectangle(ValidX,ValidY,ValidWidth,ValidHeight);
                return rect;
            }

        }


        public GridPacker()
        {
                
        }

        public List<Rectangle> GetResults(Container container, List<Rectangle> doorsList, int cabinetWidth,int cabinetHeight)
        {

            Rectangle room = container.GetValidRectangle();
            double columnCount = container.ValidWidth / cabinetWidth;
            double columnRow = container.ValidHeight / cabinetHeight;

            List<Rectangle> candidateRectangleList = new List<Rectangle>();

            //计算所有候选的位置
            for (int i = 0; i < columnRow; i++)
            {
                for (int j = 0; j < columnCount; j++)
                {
                    int x = room.X + cabinetWidth * j;
                    int y = room.Y + cabinetHeight * i;
                    Rectangle rectangle = new Rectangle(x, y, cabinetWidth, cabinetHeight);
                    candidateRectangleList.Add(rectangle);
                }
            }

            //排除门的影响


            List<Rectangle> results = candidateRectangleList.FindAll(f =>
            {
                bool ret = doorsList.All(d => d.IntersectsWith(f) == false);
                return ret;
            });

            return results;
        }
    }

}
